
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#include "utils.h"
#include "tinyxml.h"
#include "ppsadapter.h"
#include "config.h"
#include "send_http_msg.h"

void add_http_report_msg_to_buff(u_char *iphbuf, int msg_flag, u_int src_ip, u_int dst_ip, u_short src_port, u_short dst_port, long time_v,u_char * user_data, int user_data_len, int dataflag)
{
    char buf[1500];
    char strsaddr[20]={0}, strdaddr[20]={0};

    pthread_t tid = pthread_self();
    FILE *fd;
    
    if (msg_flag==0)
    {
        fd = get_card_thread_fd0(tid);
    }
    else if (msg_flag==1)
    {
        fd = get_card_thread_fd1(tid);
    }
    else
    {
        fd = get_card_thread_fd2(tid);
    }
    
    if (fd <= 0)
    {
        printf(" fd is null in add_http_report_msg_to_buff() .... \n");
        return ;
    }
    
    
    assert (fd > 0);
    
    if (1 == dataflag)
    {
    	ipaddr2str(src_ip, strsaddr);
        ipaddr2str(dst_ip, strdaddr);
        fprintf(fd, "%d %s %s %d %d %ld %d %s\n", msg_flag, strsaddr, strdaddr, ntohs(src_port), ntohs(dst_port), time_v, user_data_len, user_data);
        return ;
    }

    if (user_data_len >= 1440)
    {
        //fprintf(fd, "++++++++++++++++++++++++++++++++++++\n");
        memcpy(buf, user_data, 1434);
        memcpy(buf + 1434, "\r\n\r\n", 4);
        buf[1439] = 0;

        ipaddr2str(src_ip, strsaddr);
        ipaddr2str(dst_ip, strdaddr);
    
        fprintf(fd, "%d %s %s %d %d %ld %d %s\n", msg_flag, strsaddr, strdaddr, ntohs(src_port), ntohs(dst_port), time_v, 1440, buf);
    }  
    else
    {
        memcpy(buf,user_data,user_data_len);
        buf[user_data_len]=0;

        ipaddr2str(src_ip, strsaddr);
        ipaddr2str(dst_ip, strdaddr);
    
        fprintf(fd,"%d %s %s %d %d %ld %d %s\n",msg_flag, strsaddr, strdaddr, ntohs(src_port), ntohs(dst_port), time_v, user_data_len, buf);
    }
    
}

void report_http(struct iphdr* iph,u_short sport,u_short dport,u_char *usr_data,u_short usr_data_len,const char *host,const char *refer)
{
    add_http_report_msg_to_buff((u_char*)iph, 0, iph->saddr, iph->daddr, sport, dport, 0, usr_data, usr_data_len, 0);
	return;

}

void add_packet_buff_len(int msg_flag, u_int src_ip, u_int dst_ip, u_short src_port, u_short dst_port, long time_v, int len)
{
    pthread_t tid = pthread_self();
    FILE *fd;
    char strsaddr[20] = {0}, strdaddr[20] = {0};
    
    if (msg_flag == 0)
    {
        fd = get_card_thread_fd0(tid);
    }
    else if (msg_flag == 1)
    {
        fd = get_card_thread_fd1(tid);
    }
    else
    {
        fd = get_card_thread_fd2(tid);
    }
    
    if (fd <= 0)
    {
        printf(" fd is null in add_packet_buff_len() .... \n");
        return ;
    }
    
    assert(fd > 0);

    ipaddr2str(src_ip, strsaddr);
    ipaddr2str(dst_ip, strdaddr);

    fprintf(fd, "%d %s %s %d %d %ld %ld\r\n\r\n\n", msg_flag, strsaddr, strdaddr, ntohs(src_port), ntohs(dst_port), time_v, (long)len);
    return ;
    
}


